package com.tiyende.kash.loop.ui.presenter

import com.tiyende.kash.loop.common.CommonPresenter
import com.tiyende.kash.loop.configs.TiyendeApiPath
import com.tiyende.kash.loop.configs.TiyendeApiUrl
import com.tiyende.kash.loop.configs.TiyendeApiUrl.addCommonParams
import com.tiyende.kash.loop.request.IHttpCallback
import com.tiyende.kash.loop.ui.contract.IViewContract
import com.tiyende.kash.loop.utils.helper.TiyendeOrderHelper
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/9/25 13:45
 * @Annotation：贷款申请 Presenter 实现
 */
class TiyendeLoanApplyPresenter(private val iView: IViewContract.ITiyendeLoanApplyView): CommonPresenter(iView) {
    var loanDays = ""

    /**
     * 获取贷款信息
     */
    fun requestApplyDataAction(showAnim: Boolean = true){
        val params = JSONObject().addCommonParams()
        params.put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_LOAN_APPLY_CUSTOM_LIMIT_PATH)
        params.put("amount",iView.getTiyendeLoanApplyAmount())
        val applyDays = iView.getTiyendeLoanApplyDays()
        if (applyDays.isNotEmpty()){
            params.put("days",applyDays)
        }
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        doHttpPostAction(httpUrl, params,object: IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    //申贷时长
                    loanDays = obj.optString("days")
                    //设置明细
                    iView.showTiyendeLoanApplyDetailsList(obj)

                    //是否允许修改申请金额，不允许修改则不显示Seekbar
                    val chooseAmountFlag = obj.optBoolean("chooseAmountFlag",false)
                    when(chooseAmountFlag){
                        true -> {//允许选择贷款额度
                            iView.showTiyendeLoanApplyOptionalViews(obj,showAnim)
                        }
                        else -> {//不允许选择贷款额度
                            iView.showTiyendeLoanApplyNotOptionalViews(obj)
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hiddenTiyendeLoading()
            }
        })
    }

    /**
     * 创建贷款申请订单
     */
    fun createApplyOrderAction(){
        val params = JSONObject().addCommonParams().apply {
            put("path", TiyendeApiPath.TIYENDE_PATH.TIYENDE_API_LOAN_APPLY_ORDER_CREATE_PATH)
            put("amount",iView.getTiyendeLoanApplyAmount())
            put("days",loanDays)
        }
        val httpUrl = TiyendeApiUrl.getTiyendeHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.hiddenTiyendeLoading()
                val obj = data.optJSONObject("obj")
                obj?.let {
                    val orderId = it.optString("orderId")
                    TiyendeOrderHelper.queryOrderId = orderId
                }
                iView.showTiyendeLoanApplySuccess()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

}